<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page language="java" import="org.owasp.esapi.ESAPI" %>
<%@ include file="/rp/common/taglib.jsp" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <base href="<%=basePath%>">
    
    <title>${ webpageTitle }</title>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
	
	<script src="rp/scripts/boot.js" type="text/javascript"></script>
	
	<style type="text/css">
	   	
    </style>
	
  </head>
  
  <body class="body-fit">
  	
  	<sec:authorize ifAllGranted="AUTH_SUPERADMIN">
  		<input type="hidden" id="isSuper" value="1"/>
  	</sec:authorize>
  	
  	<div class="mini-splitter" style="width: 100%; height: 100%;" borderStyle="border:0;">
	    <div size="160" showCollapseButton="true">
	        
	        <div class="mini-fit">
	            <ul id="channelTree" class="mini-tree" url="rp/infopub/channelMng/getChannels" style="width:100%;"
	                showTreeIcon="true" textField="name" idField="id" parentField="pid" resultAsTree="false"
	                onnodeselect="channelTreeNodeSelect">        
	            </ul>
	        </div>
	    </div>
	    <div showCollapseButton="false">
	        <div class="mini-toolbar" style="padding:4px;border-top:0;border-left:0;border-right:0;">                
	            
	            <a class="mini-button" onclick="openArticleForm()"><i class="fa fa-plus fa-lg"></i> 新增</a>
	            <a class="mini-button" onclick="saveRows()"><i class="fa fa-floppy-o fa-lg"></i> 保存</a>  
	            <a class="mini-button mini-button-danger" onclick="removeRows()"><i class="fa fa-trash-o fa-lg"></i> 删除</a>     
	                         
	        </div>
	        <div class="mini-fit">
	            <div id="grid" class="mini-datagrid" style="width:100%;height:100%;" 
	                borderStyle="border:0;" pageSize="20"
	                url="rp/infopub/articleMng/getArticles"
	                allowCellSelect="true" multiSelect="true" allowCellEdit="true"
	                showColumnsMenu="true">
	                <div property="columns">
	                	<div type="indexcolumn" headerAlign="center">序号</div>
		        		<div type="checkcolumn"></div>
	                    <div field="title" width="140" headerAlign="center">标题
	                        <input property="editor" class="mini-textbox" style="width:100%;"/>
	                    </div>
	                    <div field="summary" width="200" headerAlign="center">摘要                      
	                        <input property="editor" class="mini-textbox" style="width:100%;"/>
	                    </div> 
	                    <div field="channelName" headerAlign="center">
	                    	所属栏目
	                    </div> 
	                    <div field="clickNum" width="40" headerAlign="center">
	                    	点击量
	                    </div>
	                    <div type="comboboxcolumn" autoShowPopup="true" 
				        	name="enabled2" field="enabled2" vtype="required" width="50" align="center" headerAlign="center" renderer="onEnabledRenderer">
				        	是否激活
			                <input property="editor" class="mini-combobox" style="width:100%;" data="Activates" />                
			            </div>         
	                    <div width="130" headerAlign="center" renderer="onActionRenderer">
	                    	操作
	                    </div>
	                    <div field="createDate" width="80" headerAlign="center" allowSort="true" renderer="onDateRenderer">
				        	创建日期
				        </div>  
	                </div>
	            </div>  
	        </div>
	    </div>        
	</div>
  	
	<script type="text/javascript">
		var grid;
	
		$(function(){
			mini.parse();
			grid = mini.get("grid");
			grid.load();
		});
		
		function openArticleForm(_articleId){
			var url = "";
			mini.open({
                url: "rp/infopub/articleMng/showArticleForm",
                title: "<i class='fa fa-edit fa-lg'></i> 文章编辑", 
                width: 1024, 
                height: 560,
                showMaxButton: true,
                onload: function () {
                	if(_articleId){
                		var iframe = this.getIFrameEl();
                		iframe.contentWindow.initData(_articleId);
                	}
                    //var iframe = this.getIFrameEl();
                    //var data = { action: "new"};
                    //iframe.contentWindow.SetData(data);
                },
                ondestroy: function (action) {
					grid.reload();
                }
            });
		}
		
		function saveRows() {
        	grid.validate();
			if (grid.isValid() == false) {
                var error = grid.getCellErrors()[0];
                grid.beginEditCell(error.record, error.column);
                return;
            }
			var data = grid.getChanges();
            var json = mini.encode(data);
            grid.loading("保存中，请稍等......");
            rp_ajax("rp/infopub/articleMng/saveArticles", {data: json}, function(text){
            	grid.reload();
            });
        }
		
		function removeRows() {
			mini.confirm("确定删除记录吗？", "友情提示",
	            function (action) {
	                if (action == "ok") {
	                	var rows = grid.getSelecteds();
	                    var json = mini.encode(rows);
	                    if (rows.length > 0) {
	                    	rp_ajax("rp/infopub/articleMng/deleteArticles", {data: json}, function(text){
	                    		//grid.removeRows(rows, false);
	                    		grid.reload();
	                        });
	                    }
	                }
	            }
	        );
        }
		
		function onActionRenderer(e){
			var actions = "";
			var record = e.record;
			var id = record.id;
			var title = record.title;
			if(id){
				var getIdLinkHtml = "";
				if($("#isSuper").val() == 1){
					getIdLinkHtml = "&nbsp;<a class='mini-button mini-button-small' title='超管特权' onclick='mini.alert(&#39;"+id+"&#39;)'>获取ID</a>";
				}
				actions = "<a class='mini-button mini-button-small' onclick='showArticleDetail(&#39;"+title+"&#39;,&#39;"+id+"&#39;)'><i class='fa fa-eye fa-lg'></i> 预览</a>" + 
					"&nbsp;<a class='mini-button mini-button-small' onclick='openArticleForm(&#39;"+id+"&#39;);'><i class='fa fa-edit fa-lg'></i> 编辑</a>"+getIdLinkHtml;
			}
			return actions;
		}
		
		function showArticleDetail(_title, _id){
			var title = "";
			rp_ajax("rp/system/common/encodeStr", {str: _title}, function(text){
				title = text;
				window.parent.addMainTab("文章："+title, "rp/infopub/articleMng/showArticleDetail?articleId="+_id);
			});
			
		}
		
		function channelTreeNodeSelect(e){
			var node = e.node;
			grid.load({channelId: node.id});
		}
		
		
	</script>
  </body>
</html>
